/*
 *  stores.c
 *  PriceCompareDS
 *
 *  Created by Stacy Haponik on 5/27/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include <stdio.h>
#include <string.h>
#include <fat.h>
//#include <curses.h>
#include <nds.h>

#ifndef utilities.c
#define utilities.c
#endif

int maxNameSize=20;
int maxLocationSize=100;
int maxNotesSize=100;
int storeCount=0;

typedef struct{
	char name[20];
	char location[100];
	char notes[2];
} aStore;

aStore arrayOfStores[20];										//FIX THIS: am getting stuck on figuring out how to set the size of this array appropriately, so am making it size 20.

int numberOfStores(void){					//determines how many stores are in stores.txt
	FILE *myFile;
	myFile=fopen("fat:stores.txt", "r"); 
	if (!myFile){
		iprintf("Couldn't open stores.txt for reading\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3");
		return 0;
	}
	else {
		return numberOfLinesInFile(myFile)-1;
	}
}

int readInStores(void){							//copy all stores into memory
	FILE *myFile;
	myFile=fopen("fat:stores.txt", "r"); 
	if (!myFile){
		iprintf("Couldn't open stores.txt for reading.");
		return 0;
	}
	else {
		storeCount=numberOfStores();
		//		iprintf("Number of Stores: %d\n",storeCount);
	}
	
	char myChar;
	myFile=fopen("fat:stores.txt", "r");
	//myStores arrayOfStores[storeCount];			//make an array of store structs, of size to fit all the stores
	int j=0;
	myChar = fgetc(myFile);
	while ( myChar!='\n'){		//loop through till the end of the first line, since it's just a header
		myChar = fgetc(myFile);
	}
	for (j=0; j < storeCount; j++){			//loop until all stores are added to the arrayOfStores
		int i=0;
		int exit=0;
		//	iprintf("\nreadin loop\n");
		for (i=0; i < maxNameSize - 1&&exit==0; i++){		//add the store name to the struct
			char myString[maxNameSize];
			/*				int k=0;
			 for(k=0;k<maxNameSize;k++){
			 myString[k]='0';
			 }
			 */
			myChar = fgetc(myFile);
			if ((myChar == '\t')||( myChar=='\n')){
				myString[i]='\0';
				strcpy(arrayOfStores[j].name,myString);
				//					iprintf("A Name: %s\n",myString);
				exit=1;
			}
			else myString[i]=myChar;
		}	//add name to struct
		exit=0;
		for (i=0; i < maxLocationSize - 1&&exit==0; i++){		//add the store location to the struct
			char myString[maxLocationSize];
			/*				int k=0;
			 for(k=0;k<maxLocationSize;k++){
			 myString[k]='0';
			 }				
			 */
			myChar = fgetc(myFile);
			//		iprintf("adding location\n");
			if ((myChar == '\t')||( myChar=='\n')){
				myString[i]='\0';
				strcpy(arrayOfStores[j].location,myString);
				exit=1;
			}
		}	//add location to struct
		exit=0;
		for (i=0; i < maxNotesSize - 1&&exit==0; i++){		//add the store notes to the struct
			char myString[maxNotesSize];
			/*				int k=0;
			 for(k=0;k<maxNameSize;k++){
			 myString[k]='0';
			 }
			 */
			myChar = fgetc(myFile);
			//		iprintf("adding notes\n");
			if ((myChar == '\t')||( myChar=='\n')){
				myString[i]='\0';
				strcpy(arrayOfStores[j].notes,myString);
				exit=1;
			}
		}	//add notes to struct
	}	
	fclose(myFile);
	return 0;
}		//copy all stores into memory

int printStores(void){
	readInStores();
	//	iprintf("StoreCount according to printStores: %i\n",storeCount);
	int i=0;
	for (i=0; i<storeCount; i++){	//loop until we reach the last store
		iprintf("%d: %s\n",(i+1),arrayOfStores[i].name);
	}
	return 0;
}

/*
 void keyboardGetStringAndPrintChar(char * buffer, int maxLen){			//"borrowed" from keyboard.h, then modified to print the characters input
 char *end = buffer + maxLen;
 char c;
 
 while(buffer < end){
 c = (char)keyboardGetChar(); 
 if(c == 10){				//magic number comes from keyboard.h-- equivalent to the "enter" key.
 break;
 }
 //		iprintf("%c",c);
 *buffer++ = c;
 }
 *buffer = 0;
 }	
 
 
 int addStore(void){		//append a new store to the end of stores.txt, based on user input
 keyboardDemoInit();
 keyboardShow();
 //	int key = keyboardUpdate();
 
 FILE *myFile=fopen("fat:stores.txt", "a"); 
 int bufferSize=maxNameSize+maxLocationSize+maxNotesSize+4;
 char buffer[bufferSize];
 
 iprintf("Store Name? \n");
 keyboardGetStringAndPrintChar (buffer, maxNameSize);
 char name[maxNameSize];
 strncpy(name,buffer,maxNameSize);
 
 iprintf("Location? \n");
 keyboardGetStringAndPrintChar (buffer, maxLocationSize);
 char location[maxLocationSize];
 strncpy(location,buffer,maxLocationSize);
 
 iprintf("Notes? \n");
 keyboardGetStringAndPrintChar (buffer, maxNotesSize);
 char notes[maxNotesSize];
 strncpy(notes,buffer,maxNotesSize);
 
 fprintf(myFile,"\n%s\t%s\t%s",name,location,notes);
 fclose(myFile);
 return 0;
 }
 
 int nextStore(){
 char Name[maxNameSize];
 char Location[maxLocationSize];
 //	(void) scanf("%2d%f%*d %[0123456789]", &i, &x, name);
 return 0;
 }
 */
/*
 int removeStore(void){
 return 0;
 }
 */

int storeScreen(){
	printStores();
	int addStore=0;	//if true, then we should display the "add store" keyboard and questions.
	int i=0;		//iterator to store our spot in the "add store" buffer.
	int question=1;		//which question are we on?
	int maxQuestion=3;	//how many questions do I have?
	int prompt=0;		//is it time to ask a question?
	int answerLength=0;
	int maxAnswerLength=20;	//how long of an answer should the prompt accept?
	keyboardDemoInit();
	//	keyboardHide();
	//	keyboardShow();
	
	char buffer[maxNameSize+maxLocationSize+maxNotesSize+3];	//add store buffer-- size is maximum possible size for a line to be saved to stores.txt.
	
	while(1){		//loop until you get input
		scanKeys();
		int pressed = keysDown();   // buttons pressed this loop
		
		if ( pressed & KEY_A){
			//			consoleClear();
			addStore=1;
			prompt=1;
			//			printStores();
			keyboardShow();
			iprintf("Let's add a store!");
		}		//add a store
		if ( pressed & KEY_L){
			return 1;
			iprintf("Swapping to price entries screen");
			break;
		}		//swap to "price entries" screen
		if ( pressed & KEY_R){
			return 3;
			iprintf("Swapping to Calculator screen");
			break;
		}		//swap to calculator mode
		int key = keyboardUpdate();
		//		iprintf("\nAdd Store: %i, Prompt: %i Key: %c", addStore,prompt,key); // It gets here
		
		if (addStore==1){
			//			consoleSelect(&bottomScreen);
			if (prompt==1){
				prompt=0;
				switch (question){
					case 1:
						iprintf("\nStore: ");
						maxAnswerLength=maxNameSize;
						break;
					case 2:
						iprintf("\nLocation: ");
						maxAnswerLength=maxLocationSize;
						break;
					case 3:
						iprintf("\nNotes: ");
						maxAnswerLength=maxNotesSize;
						break;
					default:
						iprintf("\nOoops. I don't know how I got here.");
						break;
				}
			}			//print the appropriate prompt
			if (key>0&&key!=255){
				//					iprintf("%c", key);
				if ((key==10)||(answerLength>=maxAnswerLength)){
					buffer[i]='\t';
					prompt=1;
					question++;
					i++;
					answerLength=0;
					//					iprintf("Adding a tab");
				}		//if there's a return character
				else if(key>=32){
					buffer[i]=key;
					iprintf("%c",key);
					i++;
					answerLength++;
				}		//if there's a letter, number, or space
				else if(key==8){
					i--;
					answerLength--;
					iprintf("%c",key);
				}		//if there's a backspace character
				if (question>maxQuestion){
					i--;		//remove the trailing tab character
					buffer[i]='\0';
					question=1;
					addStore=0;
					i=0;
					answerLength=0;
					FILE *myFile=fopen("fat:stores.txt", "a"); 
					fprintf(myFile,"\n%s",buffer);
					fclose(myFile);
					consoleClear();
					iprintf("\nAdded store: %s\n",buffer);
					printStores();
					keyboardHide();
				}				//print to file, reset iterators
				key=0;
			}
			//			consoleSelect(&topScreen);
		}		//loop to add a store  
		swiWaitForVBlank();
	}
}